<?php $this->display(tpl('head')); ?>
<fieldset class="layui-elem-field layui-field-title">
  	<legend>钩子API接口管理</legend>
  	<div class="layui-row mt10">
  		<a class="layui-btn  layui-btn-normal" href="javascript:" onclick="add('0')">添加钩子api</a>
  		<a class="layui-btn  layui-btn-normal" href="/admin/hook/index/" >返回钩子</a>
  	</div>
</fieldset>
<style>
	.my-tabel tbody tr td:first-child {
    	width: 0;
	}
	.my-tabel thead tr th{
		text-align: center;
	}
	.mt10{
		margin-left: 20px;
		margin-top: 10px;
	}
	.my-tabel thead tr th:last-child{
		width: 120px;
	}

</style>
 <table class="layui-table my-tabel layui-form">
 	<thead>
		<tr>
		  	<th style="width: 10px;">id</th>
		    <th width="100">钩子类</th>
		    <th width="100">执行方法</th>
		    <th>钩子描述</th>
		   	<th width="100">归属接口</th>
		   	<th width="100">开发者</th>
		   	<th width="100">状态</th>
		   	<th width="80">操作</th>
		</tr>
	</thead>
	<tbody>
		<?php foreach($data as $row)	{ ?>
		<tr id="list_<?php echo $row[id] ?>">
			<td align="center"><?php echo $row[id] ?></td>
			<td align="center" ><?php echo $row['class'] ?></td>
			<td align="center" ><?php echo $row['method'] ?></td>
 			<td><?php echo $row[content] ?></td>
 			<td align="center" title="<?php echo $row[hook][content] ?>"><?php echo $row[hook][name] ?></td>
 			<td>
 				<?php echo $row[developer] ?>
 			</td>
   			<td align="center">
   				<input type="checkbox" id="<?php echo $row[id] ?>" <?php if($row[state]) echo 'checked'?> name="state"  lay-filter="state" lay-skin="switch"  lay-text='开启|关闭'>
   			</td>
			<td align="center">
				<a class="layui-btn layui-btn-xs layui-btn-normal" href="javascript:" onclick="add(<?php echo $row[id] ?>)">编辑</a>
				<a class="layui-btn layui-btn-xs layui-btn-danger"  onclick="return confirm('您是否确定要删除本钩子API？此方法不可逆')"  href="/admin/hook/hook_list/step_del/id_<?php echo $row[id] ?>">删除</a>
			</td>
		</tr>
		<?php } ?>
	</tbody>
</table>
<div class="page"><?php echo $Get_Page ?></div>
<div class="post" style="display: none;">
	<form class="layui-form" action="" method="post">
		<table class="layui-table">
			<tbody>
				<tr>
					<td style="width: 40px;">钩子类名：</td>
					<td>
						<input type="text" name="class" class="layui-input" id="class"/>
						<p>命名空间+类名,如：\hook\temmoku或 \hook\temmoku::run()</p>
					</td>
				</tr>
				<tr>
					<td style="width: 40px;">执行方法：</td>
					<td>
						<input type="text" name="method" class="layui-input my-input" id="method"/>
						<p>可不填写</p>
					</td>
				</tr>
				<tr>
					<td style="width: 40px;">归属钩子预埋点：</td>
					<td>
						<select name="hook_name" id="hook_name">
							<option value="">请选择一个预埋点</option>
							<?php foreach($hook as $row){ ?>
								<option value="<?php echo $row[name] ?>"><?php echo $row[name] ?>(<?php echo $row[content] ?>)</option>
							<?php } ?>
						</select>
					</td>
				</tr>
				<tr>
					<td style="width: 40px;">钩子描述：</td>
					<td>
						<textarea class="layui-textarea" id="content" name="content"></textarea>
					</td>
				</tr>
				<tr>
					<td style="width: 40px;">状态：</td>
					<td>
						<input type="radio" name="state" id="state1" value="1" title="开启"/>
						<input type="radio" name="state" id="state0" value="0" title="关闭"/>
						<p>只有开启状态才有效</p>
					</td>
				</tr>
				<tr>
					<td style="width: 40px;">钩子排序：</td>
					<td>
						<input type="number" name="reorder" id="reorder" class="layui-input my-input"/>
						<p>数值大的优先执行</p>
					</td>
				</tr>
				<tr>
					<td colspan="2" style="text-align: center; width: 100%;">
						<input type="hidden" name="id" id="id" value="0" />
						<input type="hidden" name="step" id="step" value="post" />
						<input type="hidden" name="json" id="json" value="true" />
    					<button class="layui-btn" lay-submit lay-filter="post">提交</button>
    					<button type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
					</td>
				</tr>
			</tbody>
		</table>
	</form>
</div>
<script type="text/javascript">
	var form=Object;
	function add(id){
		$('#id').val(id);
		if(id){
			$.ajax({
				url: "",
				type: 'POST',
				data: {'json':true,'step':'one','id':id},
				dataType: 'json',
				success: function(res) {
					if(res.code==0){
						$('#class').val(res.data.class);
						$('#content').val(res.data.content);
						$('#reorder').val(res.data.reorder);
						$('#method').val(res.data.method);
						$('#hook_name').val(res.data.hook_name);
						if(res.data.state){
							$('#state1').click();
						}else{
							$('#state0').click();
						}
						form.render();
					}
				}
			});
		}
		layer.open({
			type: 1,
			title: '添加&修改钩子API',
			area: ['80%', '400px'],
			shadeClose:false,
			hade: [0.8, '#393D49'],
			content: $('.post') //这里content是一个普通的String
		});
		return false;
	}

	layui.use(['form','table'], function(){
		 form = layui.form;
		var table = layui.table;
		form.on('submit(post)', function(data){
			$.ajax({
				url: "",
				type: 'POST',
				data: data.field,
				dataType: 'json',
				success: function(res) {
					if(res.code==0){
						window.location.reload();
					}
					layer.msg(res.text);
				}
			});
			return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
		});
		form.on('switch(state)', function(data){
			var id=data.elem.id;
			var state=1;
			if(data.elem.checked ==false){
				state=0;
			}
			$.ajax({
				url: "../hook_state/",
				type: 'POST',
				data: {state:state,'id':id,'json':true},
				dataType: 'json',
				success: function(res) {
					layer.msg(res.text);
				}
			});
		});  
	});
</script>
<?php $this->display(tpl('foot')); ?>
